﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace EncryptConfig
{
    public partial class EncryptConfig : Form
    {
        public EncryptConfig()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Select exe file for the config file you want to encrypt";
            fDialog.Filter = "EXE Files|*.exe";
            fDialog.InitialDirectory = @"C:\";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
               textBox2.Text = fDialog.FileName.ToString();
               if (isProtected(fDialog.FileName.ToString()))
               {
                   button1.Text = "Decrypt";
                   label2.Text = "File is encrypted";
               }
               else 
               {
                  button1.Text = "Encrypt";
                  label2.Text = "File is decrypted";
               }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EncryptConfigFile(textBox2.Text);
        }

        private void EncryptConfigFile(string exeConfigName)
        {
            // Takes the executable file name without the
            // .config extension.
            try
            {
                // Open the configuration file and retrieve 
                // the connectionStrings section.
                Configuration config = ConfigurationManager.
                    OpenExeConfiguration(exeConfigName);

                ConnectionStringsSection section =
                    config.GetSection("connectionStrings")
                    as ConnectionStringsSection;

                if (section.SectionInformation.IsProtected)
                {
                    // Remove encryption.
                    section.SectionInformation.UnprotectSection();
                    button1.Text = "Encrypt";
                    label2.Text = "File is decrypted";
                }
                else
                {
                    // Encrypt the section.
                    section.SectionInformation.ProtectSection(
                        "DataProtectionConfigurationProvider");
                    button1.Text = "Decrypt";
                    label2.Text = "File is encrypted";
                }
                // Save the current configuration.
                config.Save();

                Console.WriteLine("Protected={0}",
                    section.SectionInformation.IsProtected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private bool isProtected(string exeConfigName)
        {
            bool blnProtected = false;
            // Takes the executable file name without the
            // .config extension.
            try
            {
                
                
                // Open the configuration file and retrieve 
                // the connectionStrings section.
                Configuration config = ConfigurationManager.
                    OpenExeConfiguration(exeConfigName);

                ConnectionStringsSection section =
                    config.GetSection("connectionStrings")
                    as ConnectionStringsSection;

                if (section.SectionInformation.IsProtected)
                {
                    blnProtected = true;

                }
                else 
                {
                    blnProtected = false;
                }
               
            }
            
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return blnProtected;
        }
    }
}
